package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
@SuppressWarnings("all")
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2016092200568607";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCCdxGrSarigM+OX1zBYl3dCqG7BR7idqGkoPaDDIxGf7zxRhuomKLIkKdQuwKaPOwCvvp+XT7pNM+ctQLV9+XcQXjpaxoekRl5/MBB8xw1uJ8xa3IvMM+Wy2m4yozX42wwowL6CKV8OKFniR5850jLHAZopBfCtR3XiHn0pi+PRM0fGd/svWkvYhAO+D6rcsdaBsqNzN9dngTLKQYcQksyIoVJDP9WCCA82VVOFdGV87kGjJPyqtYGLtYYBq3XGOay/5CAqaRjbtzAs1DLTo28Ak5Mho4OXagU3+mWvM6nYjRvUG8UubtJDNJWqtmcX7h17A6R0vj0yjIPp+Fq0I7XAgMBAAECggEAKW28C6NyA0kaH8DCiJo+6jE1GFlQ7GL1g3ylqwmyBbUMf6VepRQLJRtiNi2cbvQHkfupObVMg+uEPePxCZcVw/J6fczH14sGBwmu14Jmo9yDmMLg2IFsehsP8GFJhkiuk2dAS7uxKRgmkOUm7EzZfb3hfxsUfHzdBBNPftPC6IBhDQ46GxB70ZxWZizhIeqzK15b01JEJ6w0RlQtD84jUr0UGwDPE3c16B5RVswLbz9ZBlRqfrlNjTkm06hjouxiB4nLxBYbOFnROVFHHK7iN+5hrz2LwLOcbCT0hn+V9czKoyyqOqA/d5Tfs2qwuR3keOfnM+ctd9rfE6Q0fz+j2QKBgQDHWFEsKr72H2FV/e9i06PspYf1gjQVupv8KpEJ19vDKwwTqaPr6jbSUw8K0TPRFxbzOT9gXoVHMw+4LkUeb/gAU98Ni/cjiwOqoqrED6Zvf3f4oWvurQ+8VCWgzTTQBxbjC8IdHJ3QVjH6XgfJ/Q0oCWjrNoo7oKar13ebPeAoFQKBgQCni0ecjd3XJ/awXe0Hmc/mG3/DxB0Oc0mHYm4k72WYSpd16jC/bN85hrBnVSt8gzUXgqozWwXBhhIQjm3u3lbN5KiVYmzpZlyWUjV36FjTbEYd8tqenUfOQfJuVlEjVAFbmWtLMy+Pxjv4Z+ifXTVXcn+RxkAmFc3yHoM0/1yKOwKBgQC4dXWvO+K2VnU4ICC6PwKJgOac/B18GdAq96x8Ajz9jMDBFRF0xKZz2p2pscmT6hy3dbq6Ttxe9NXP5SjaMFu42eMlYuWOAx2icKE5iQ3GUJpP+/amxYcxCmBDOn9rG5RHBeNaEtfJJ2lZtlbYxoq9CHAIbtBcupxfmKK5iPj5vQKBgQCGuVXFSsK3T1hr66s9MG7frv8nEVz4ELZwmjLNmn+MXXa7h6zZUReS736MKQ2F2w/nLtfj4e6X9lIyZAV8TedpOtRhAztm1zveWI7MM9axc+BSFsW7Q+fIsHjcRqaKDPPmj1CasLcvGo2AqzWAaHgLMHPqaYuN1Hy9k9GX3TuxnwKBgBKbljsKVv7ULnTAN32Vmr/AAL26oAtl02dn3BTn+hCVOrRDcH6Be+i3kHVwZGUn+tqzUEPGaTvHyGQ57zdqbmivkucbBjPo+1/kogNkSWhxowDROfKhI7V0IjNzWMook61o+7SrdRBP6mKGPjxWBCxHKo4/kfjEofYlMUiCM3Jq";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAh8Qg7swWqYFse0qdtVq68m23PVFSNM/4AFzdStdYxNWWLuBdOpJ06Lcc5Q4rW5jVqFZGlhckZCjXyQX/0kvD2ZZg+F7De20Vw4s7Wil0IIHt84C5L847A1017EBOnXpVUfxvNpZGHodV4E+I68ppKm+wl05d2WulIyV0tQQjUAsWZsg6XlzNjl1lZ4JOn+NmwrnNFrWtB7xZrFD9WjzF6N5Xt2RdWJj30TNqPuy/ygpfbdFdAtzQWdXQy+1QrpoLQBicgplVplT6nVuRLiX6p82Kr6McByPLLyLqz/4cVGdacUB6jIh05HxxLchv00sa1oI34eGd7L9zU0Dm8JAzlQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http:member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
